﻿using System;
using System.Collections.Specialized;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using Agnes.Core;
using DevExpress.Xpf.Bars;
using System.Collections.ObjectModel;
using DevExpress.Xpf.Docking;

namespace Agnes.Mainframe
{
    /// <summary>
    /// MainframeView.xaml 的交互逻辑
    /// </summary>
    public partial class MainframeView : UserControl
    {
        private ICommand _docClosedCmd = new DocCloseCmd();

        public MainframeView()
        {
            InitializeComponent();
            foreach (BarItem i in AgnesServiceManager.AgnesWorkBenchService.BarItems)
            {
                DxBarManager.Items.Add(i);
            }

            AgnesServiceManager.AgnesWorkBenchService.PropertyChanged 
                += (sender, args) =>
                {
                    if (args.PropertyName == "FocusedRichEditControl")
                    {
                        if (AgnesServiceManager.AgnesWorkBenchService.FocusedRichEditControl != null)
                        {
                            AgnesServiceManager.AgnesWorkBenchService.FocusedRichEditControl.BarManager = DxBarManager;
                        }
                        else
                        {
                            RECDefaultRichEditControl.BarManager = DxBarManager;
                        }
                    }
                };


            foreach (IDocumentPanel i in AgnesServiceManager.AgnesWorkBenchService.DocumentPanels)
            {
                AddDocumentPanel(i);
            }

            AgnesServiceManager.AgnesWorkBenchService.DocumentPanels.CollectionChanged
                += (sender, args) =>
                       {
                           if (args.Action == NotifyCollectionChangedAction.Add)
                           {
                               foreach (IDocumentPanel i in args.NewItems)
                               {
                                   AddDocumentPanel(i);
                               }
                           }
                           else if (args.Action == NotifyCollectionChangedAction.Remove)
                           {
                               foreach (IDocumentPanel i in args.OldItems)
                               {
                                   foreach (BaseLayoutItem j in documentContainer.Items)
                                   {
                                       if (j.DataContext == i)
                                       {
                                           documentContainer.Remove(j);
                                           break;
                                       }
                                   }
                               }
                           }
                       };
        }

        void AddDocumentPanel(IDocumentPanel panel)
        {
            DocumentPanel documentPanel = new DocumentPanel();
            documentPanel.DataContext = panel;
            Binding binding = new Binding("Caption");
            documentPanel.SetBinding(DocumentPanel.CaptionProperty, binding);
            documentPanel.Content = panel.Content;
            documentPanel.CloseCommand = new DocCloseCmd();
            documentContainer.Items.Add(documentPanel);
            documentContainer.ClosingBehavior = ClosingBehavior.ImmediatelyRemove;
            documentPanel.GotFocus += (sender, args) =>
                                          {
                                              AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel =
                                                 (sender as DocumentPanel).DataContext as IDocumentPanel;
                                          };
            documentPanel.LostFocus += (sender, args) =>
                                           {
                                               AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel = null;
                                           };
            documentPanel.Focus();

        }
    }

    class DocCloseCmd : ICommand
    {

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            SaveDialog sd = new SaveDialog();
            sd.JustActiveDocument = true;
            sd.ShowSDialog();
            if (sd.SaveResult != SaveResult.Cancel)
            {
                AgnesServiceManager.AgnesWorkBenchService.DocumentPanels.Remove((parameter as DocumentPanel).DataContext as IDocumentPanel);
            }
        }
    }
}
